How to Install Python 3.8 on Ubuntu 22.04 or 20.04

您所在的位置:网站首页 ubuntu python 38 How to Install Python 3.8 on Ubuntu 22.04 or 20.04

How to Install Python 3.8 on Ubuntu 22.04 or 20.04

2023-03-17 02:50| 来源: 网络整理| 查看: 265

Python 3.8 is an older release of the Python programming language that is now in security maintenance. Despite being an older release, Python 3.8 is still widely used and is known for its simplicity, flexibility, and readability. Here are some of the key features of Python 3.8:

Improved performance: Python 3.8 has several performance optimizations, including faster string formatting, improved dictionary, and set operations, and faster sorting algorithms. These optimizations make it easier to write efficient and scalable code. Enhanced syntax: Python 3.8 introduces several new syntax features, including f-strings for more concise string formatting and the walrus operator for more concise and expressive code. Better security: Python 3.8 includes improved TLS support, hash algorithms, and other security enhancements. Positional-only parameters: Python 3.8 introduces a new syntax for defining positional-only parameters in functions, making writing more expressive and concise code easier. Debugging improvements: Python 3.8 includes several improvements to the debugging experience, including better traceback formatting and a new breakpoint() function. Type hints: Python 3.8 includes improved support for type hints, which can help make code more readable and maintainable.

Suppose you want to use Python 3.8 on Ubuntu 22.04 Jammy Jellyfish or Ubuntu 20.04 Focal Fossa LTS. In that case, you can install it using the command line terminal and the Python PPA or download and install it manually. However, it’s worth noting that newer releases of Python are available, and if you’re starting a new project, it’s generally recommended to use the latest stable release.

Update Ubuntu

Running an update in your terminal before installing Python 3.8 is highly recommended to avoid any potential conflicts during installation. This will ensure that all packages are up-to-date using the following command:

sudo apt update

If you wish, you can list the updates to review or for those curious about them.

sudo apt --list upgradable

You can upgrade any outdated packages by using the following command.

sudo apt upgrade Method 1: Install Python 3.8 with LaunchPAD PPA

For Ubuntu users, the simplest solution is to import the “deadsnakes” team Launchpad PPA, which will provide access to the latest updates for Python and any additional required packages.

Import Python 3.8 Repository

First, install the following packages that are required. These are most likely installed but run the command to be safe.

sudo apt install ca-certificates apt-transport-https software-properties-common lsb-release -y

If you haven’t previously imported a GPG key from the Ubuntu keyserver, you may encounter issues importing GPG keys from LaunchPAD PPAs via the command line terminal. This is because the necessary directories may not have been created. Fortunately, this is an easy fix – use the following command to generate the directories.

sudo gpg --list-keys

Example output:

gpg: directory '/root/.gnupg' created gpg: keybox '/root/.gnupg/pubring.kbx' created gpg: /root/.gnupg/trustdb.gpg: trustdb created

While optional, running the command and trying again if you encounter any issues is recommended. The next step is to import the required GPG key.

sudo gpg --no-default-keyring --keyring /usr/share/keyrings/deadsnakes.gpg --keyserver keyserver.ubuntu.com --recv-keys F23C5A6CF475977595C89F51BA6932366A755776

Example output:

gpg: keybox '/usr/share/keyrings/deadsnakes.gpg' created gpg: key BA6932366A755776: public key "Launchpad PPA for deadsnakes" imported gpg: Total number processed: 1 gpg: imported: 1

Now that the GPG key has been successfully imported, you can import the LaunchPAD PPA with the following command:

echo "deb [signed-by=/usr/share/keyrings/deadsnakes.gpg] https://ppa.launchpadcontent.net/deadsnakes/ppa/ubuntu $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/python.list

Before proceeding, run an APT update to ensure that the newly imported PPA is reflected.

sudo apt update Run installation command for Python 3.8

Now that the 3.8 PPA has been successfully imported, you can install Python by running the following command.

sudo apt install python3.8

Verify the installation and build version using the following command.

python3.8 --version

Example output:

Python 3.8.15

If desired, you can also install the following additional extras.

Debug module Python 3.8 installation command sudo apt install python3.8-dbg Developer (dev) module Python 3.8 installation command sudo apt install python3.8-dev VENV (virtual environment) module Python 3.8 installation command sudo apt install python3.8-venv Distutils module Python 3.8 installation command sudo apt install python3.8-distutils lib2to3 utility module Python 3.8 installation command sudo apt install python3.8-lib2to3 DBM.GNU module Python 3.8 installation command sudo apt install python3.8-gdbm Tkinter module Python 3.8 installation command sudo apt install python3.8-tk

Alternatively, to install all extras, run the full installation command.

sudo apt install python3.8-full Method 2: Install Python 3.8 Manually Download Python 3.8

To begin, visit the official download page and download the latest version of the specific Python release you want. These instructions should work for any version, as you’ll be compiling it yourself. Once you’ve copied the download link, use the wget command to download the Python 3.8 archive.

wget https://www.python.org/ftp/python/3.8.15/Python-3.8.15.tar.xz

Note that the download link may change soon, so be sure to obtain a fresh link. The above command is just an example. After downloading the Python archive, make sure to extract it and remember to update the version number if you downloaded a newer release.

tar -xf Python-3.8.{version}.tar.xz

Next, you’ll need to install the dependencies required to install Python 3.8.

sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libsqlite3-dev libreadline-dev libffi-dev curl libbz2-dev pkg-config make -y

Go to the directory where you extracted the Python archive.

cd Python3.8.{version}/

Now, run the ./configure 鈥揺nable-optimizations command.

./configure --enable-optimizations --enable-shared

The script will perform several checks to ensure that all necessary dependencies are present on your system. Running ./configure –enable-optimizations will optimize the Python binary by running multiple tests, which can make the build process slower but will result in a faster and more efficient Python installation.

After building and configuring the environment, it’s time to compile it using the command “make”.

make

To significantly increase the compiling speed, you can use the “-j ” option, which specifies the number of CPUs you want. For example, if your server has 6 CPUs, you can use all six or at least 4 to 5 to increase the speed. The command would be “make -j 6”.

make -j 6

After finishing the building process, install the Python binaries by running the following command: “sudo make altinstall.” Using the “make altinstall” command is recommended to avoid overwriting the default Python 3 binary system.

sudo make altinstall

Finally, configure the dynamic linker run-time bindings by running the ldconfig command after installation: “sudo ldconfig”.

sudo ldconfig

Example only:

sudo ldconfig /opt/Python3.8.15

Run the following command to confirm that Python 3.8 and the corresponding build version have been successfully installed.

sudo python3.8 --version

Example output:

Python 3.8.15 Install Python PIP with 3.8 on Ubuntu Linux

For most users using the Python PPA repository, installing Python 3.8 can be accomplished simply by running the following APT command.

sudo apt install python3-pip

Pip should already be installed, but if you encounter issues and need to reinstall it manually, follow these steps to download get-pip.py using the wget command.

wget https://bootstrap.pypa.io/get-pip.py

After downloading the file, the next step is to install it.

python3.8 get-pip.py

After installation, it’s recommended to check for upgrades to ensure that you have the latest version of Pip.

python3.8 -m pip install --upgrade pip

Example output:

[email protected]x:~$ python3.8 -m pip install --upgrade pip Defaulting to user installation because normal site-packages is not writeable Requirement already satisfied: pip in ./.local/lib/python3.8/site-packages (22.3.1)

You can verify the version of Pip 3.8 that is installed by running the following command.

pip3.8 --version Switch Default Python Versions on Ubuntu Linux

If you need to have multiple versions of Python installed on your system and want to set a particular one as the default, you can follow these steps to change Python versions.

First, you’ll need to add the symbolic links for each Python version separately. Next to the symlink, add the group name “python” and the version number.

Here’s an example (you can customize this or copy it):

sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1 sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.7 2 sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 3 sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.9 4 sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.10 5 sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.11 6 sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.12 7

Example output:

example installing python alternative versions on ubuntu 22.04 or 20.04 for python 3.8

Keep in mind that you don’t need to list all the Python versions you have installed – copying the entire command will create symbolic links for the versions that are present on your system.

To list the available Python versions, use the following command.

sudo update-alternatives --config python

Example output:

list default python version and alternatives on ubuntu 22.04 or 20.04 lts for python 3.8

As mentioned, the guide machine has installed multiple Python versions (3.12, 3.11, 3.10, 3.9, 3.8, and 3.7), with 3.12 currently set as the default (indicated by an asterisk * beside the selection number).

To set a different version as the default (e.g., Python 3.8), you must enter the corresponding selection number (which will vary depending on the number of versions you have installed). In this example, the selection number for Python 3.8 is 6.

If the command is successful, you’ll see the following output:

update-alternatives: using /usr/bin/python3.8 to provide /usr/bin/python (python) in manual mode

After running the command to set Python 3.8 as the default version, if you list the available alternative options again, you should see that Python 3.8 is now the default version (indicated by an asterisk *).

Example:

example switching to python 3.8 from python 3.12 on ubuntu 22.04 or 20.04 lts Conclusion

This guide has shown two methods to install Python 3.8 on Ubuntu, which can be chosen based on personal preference and experience level. While Python 3.8 is still a powerful language and offers many useful features, it is important to note that it is an older release and is now in security maintenance mode. It is recommended that users upgrade to Python 3.11 or higher as soon as possible to take advantage of the latest features and security updates. Python 3.11 is the latest stable release, with Python 3.12 currently in development. Regardless of the version chosen, Python remains a popular and useful programming language for various applications, from web development to data science and machine learning.

FAQs on Python 3.8 with Ubuntu Q: What are the new features in Python 3.8 compared to Python 3.7 and earlier versions?

A: Python 3.8 introduced several new features, including positional-only parameters in functions, assignment expressions, and f-strings improvements. Additionally, Python 3.8 added a new syntax for type annotations and several new built-in modules.

Q: Can I use Python 3.8 with Django on Ubuntu?

A: Yes, you can use Python 3.8 with Django on Ubuntu. Django has been compatible with Python 3 since version 1.5, and Python 3.8 is fully supported by Django 2.2 and later.

Q: Is Python 3.8 compatible with TensorFlow on Ubuntu?

A: Yes, Python 3.8 is compatible with TensorFlow on Ubuntu. TensorFlow has been compatible with Python 3 since version 1.5, and Python 3.8 is fully supported by TensorFlow 2.4 and later.

Q: What are the best practices for using Python 3.8 on Ubuntu?

A: Some best practices for using Python 3.8 on Ubuntu include using a virtual environment to manage dependencies, keeping your Python installation up-to-date, and following Python coding style guidelines. Additionally, it’s recommended to use a package manager like apt or Pip to install Python packages and to test your code thoroughly before deploying it.



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3